const resolveCwd = require('./resolveCwd')
const jsonUtil = require('./jsonUtil');
const EXIT_CODE = require('./exitCode');
const { exitSuccess, exitError } = require('./exit')

module.exports = function getPkgScript(scriptKey) {
  const pkgDir = resolveCwd('package.json')
  let data = null

  try {
    data = jsonUtil.readData(pkgDir)
  } catch {
    exitError(EXIT_CODE.LACK_PACKAGE)
  }

  const val = data.scripts[scriptKey]
  if (val != undefined) {
    exitSuccess()
  } else {
    exitError(EXIT_CODE.LACK_SCRIPTKEY)
  }
};
